package leetcode.code1423;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

//执行用时：2 ms, 在所有 Java 提交中击败了74.62%的用户
//
//内存消耗：49.3 MB, 在所有 Java 提交中击败了32.10%的用户
//
//通过测试用例：40 / 40
public class Solution4 implements IDebug {

	public int maxScore(int[] cardPoints, int k) {
		int len = cardPoints.length;
		int total = 0;
		int[] sums = new int[len + 1];
		for (int i = 1; i <= len; i++) {
			int num = cardPoints[i - 1];
			total += num;
			sums[i] = sums[i - 1] + num;
		}
		int rest = len - k;
		int restSum = sums[rest];
		int min = restSum;
		for (int i = rest + 1; i <= len; i++) {
			restSum = sums[i] - sums[i - rest];
			min = Math.min(min, restSum);
		}
		return total - min;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(202, this.maxScore(HelpDebug.str2array3("[1,79,80,1,1,1,200,1]"), 3));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution4 so = new Solution4();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
